/*
 * setPCBPriority.c
 *
 *  Created on: Oct 2, 2013
 *      Author: Joseph DeLong
 */

#include <stdlib.h>
#include "mpx_pcb.h"

/*
 * SET PCB PRIORITY
 * Written by Joseph DeLong
 * Debugged by Alex Newcomer
 * Calls find_PCB(), sets the new priority
 * Parameters: Process Name
 * Error Checking:
 * 	Name must be valid
 * 	New priority must be valid (-128 to +127)
 * 	Tip: You may need to remove and re-insert the pcb to maintain priority order.
 * Should display success or error message
 */
void setPCBPriority(char processName[9], int priority, struct MPX_Q *r, struct MPX_Q *b) {
	struct MPX_PCB *pcb;
	pcb = findPCB(processName);
	if(pcb == NULL) { //couldn't find PCB
		printf("Couldn't find PCB %s to set to priority  %i.\n", processName, priority);
	}
	else { //PCB found!
		if(priority < -128 || priority > 127) { //improper priority value
			printf("Invalid value for priority: %1. Value must be between -128 and 127.\n",priority);
		}
		else { //got good priority value
			if((*pcb).priority == priority) { //PCB priority == passed-in value
				printf("PCB %s's priority is already at value %i.\n", processName, priority);
			}
			else {
				(*pcb).priority = priority; //set new priority
				if((*pcb).pcb_state != -1) { //if PCB is in ready queue
					removePCB(pcb,r,b); //remove from queue
					insertPCB(pcb,r,b); //re-insert into queue
				}
				printf("PCB %s's priority has been set to $i.\n", processName, priority);
			}
		}
	}
}
